/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: variant_internal.h
 *
 * Purpose: variant internal helper function implementation
 *
 * Developer:
 *   wen.gu , 2020-04-16
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_CORE_INTERNAL_VARIANT_INTERNAL_H__
#define __ARA_CORE_INTERNAL_VARIANT_INTERNAL_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#include <type_traits>
#include <cstdint>

#include "ara/core/utility.h"
#include "ara/core/internal/recursive_wrapper.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace core
{
namespace internal_impl
{

using type_index_t = uint32_t;
constexpr type_index_t invalid_value = type_index_t(-1);
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

 /// [allocator.tag]
struct allocator_arg_t { explicit allocator_arg_t() = default; };

constexpr allocator_arg_t allocator_arg = allocator_arg_t();


/** for access the Nth type */
template<size_t _Np, typename... _Types>
struct _TypeAt;

template<size_t _Np, typename _First, typename... _Rest>
struct _TypeAt<_Np, _First, _Rest...>
    : _TypeAt<_Np - 1, _Rest...> 
{ };

template<typename _First, typename... _Rest>
struct _TypeAt<0, _First, _Rest...>
{
    using type = _First;
};


/** for check template params */
template<typename... _Types>
struct _Traits
{
    static constexpr bool _S_default_ctor = std::is_default_constructible<typename _TypeAt<0, _Types...>::type>::value;
    static constexpr bool _S_copy_ctor = Conjunction<std::is_copy_constructible<_Types>&& ...>;
    static constexpr bool _S_move_ctor = Conjunction<std::is_move_constructible<_Types>&& ...>;
    static constexpr bool _S_copy_assign = _S_copy_ctor && Conjunction<std::is_copy_assignable<_Types>&& ...>;
    static constexpr bool _S_move_assign = _S_move_ctor && Conjunction<std::is_move_assignable<_Types>&& ...>;

    static constexpr bool _S_trivial_dtor = Conjunction<std::is_trivially_destructible<_Types>&& ...>;
    static constexpr bool _S_trivial_copy_ctor = Conjunction<std::is_trivially_copy_constructible<_Types>&& ...>;
    static constexpr bool _S_trivial_move_ctor = Conjunction<std::is_trivially_move_constructible<_Types>&& ...>;
    static constexpr bool _S_trivial_copy_assign = _S_trivial_dtor && _S_trivial_copy_ctor && Conjunction<std::is_trivially_copy_assignable<_Types>&& ...>;
    static constexpr bool _S_trivial_move_assign = _S_trivial_dtor && _S_trivial_move_ctor && Conjunction<std::is_trivially_move_assignable<_Types>&& ...>;

    // The following nothrow traits are for non-trivial SMFs. Trivial SMFs
    // are always nothrow.
    static constexpr bool _S_nothrow_default_ctor = std::is_nothrow_default_constructible<typename _TypeAt<0, _Types...>::type>::value;
    static constexpr bool _S_nothrow_copy_ctor = false;
    static constexpr bool _S_nothrow_move_ctor = Conjunction<std::is_nothrow_move_constructible<_Types>&& ...>;
    static constexpr bool _S_nothrow_copy_assign = false;
    static constexpr bool _S_nothrow_move_assign = _S_nothrow_move_ctor && Conjunction<std::is_nothrow_move_assignable<_Types>&& ...>;
};


template <size_t arg, size_t... rest>
struct IntegerMax;

template <size_t arg>
struct IntegerMax<arg> : std::integral_constant<size_t, arg>
{
    //static const size_t value = arg;
    //enum{value = arg};
};

//max interger align
template <size_t arg1, size_t arg2, size_t... rest>
struct IntegerMax<arg1, arg2, rest...> : std::integral_constant<size_t, arg1 >= arg2 ? IntegerMax<arg1, rest...>::value :
    IntegerMax<arg2, rest...>::value >
{
    /*static const size_t value = arg1 >= arg2 ? static_max<arg1, others...>::value :
    static_max<arg2, others...>::value;*/
};

template<typename... Args>
struct MaxAlign : std::integral_constant<int, IntegerMax<std::alignment_of<Args>::value...>::value> {};


/** for internal helper functions */
template <typename... Types>
struct VariantHelper;

template <typename T, typename... Types>
struct VariantHelper<T, Types...>
{
    static void destroy(const type_index_t type_index, void* data)
    {
        if (type_index == sizeof...(Types))
        {
            reinterpret_cast<T*>(data)->~T();
        }
        else
        {
            VariantHelper<Types...>::destroy(type_index, data);
        }
    }

    static void move(const type_index_t old_type_index, void* old_value, void* new_value)
    {
        if (old_type_index == sizeof...(Types))
        {
            new (new_value) T(std::move(*reinterpret_cast<T*>(old_value)));
        }
        else
        {
            VariantHelper<Types...>::move(old_type_index, old_value, new_value);
        }
    }

    static void copy(const type_index_t old_type_index, const void* old_value, void* new_value)
    {
        if (old_type_index == sizeof...(Types))
        {
            new (new_value) T(*reinterpret_cast<const T*>(old_value));
        }
        else
        {
            VariantHelper<Types...>::copy(old_type_index, old_value, new_value);
        }
    }
};

template <>
struct VariantHelper<>
{
    static void destroy(const type_index_t, void*) {}
    static void move(const type_index_t, void*, void*) {}
    static void copy(const type_index_t, const void*, void*) {}
};

template <typename T, typename... Types>
struct direct_type;

template <typename T, typename First, typename... Types>
struct direct_type<T, First, Types...>
{
    static constexpr type_index_t index = std::is_same<T, First>::value  ? sizeof...(Types) : direct_type<T, Types...>::index;
};

template <typename T>
struct direct_type<T>
{
    static constexpr type_index_t index = invalid_value;
};


template <typename T, typename... Types>
struct convertible_type;

template <typename T, typename First, typename... Types>
struct convertible_type<T, First, Types...>
{
    static constexpr type_index_t index = std::is_convertible<T, First>::value
        ? Disjunction<std::is_convertible<T, Types>...>::value ? invalid_value : sizeof...(Types)
        : convertible_type<T, Types...>::index;
};

template <typename T>
struct convertible_type<T>
{
    static constexpr type_index_t index = invalid_value;
};


template <typename T, typename... Types>
struct value_traits
{
    using value_type = typename std::remove_const<typename std::remove_reference<T>::type>::type;
    using value_type_wrapper = recursive_wrapper<value_type>;
    static constexpr type_index_t direct_index = direct_type<value_type, Types...>::index;
    static constexpr bool is_direct = direct_index != invalid_value;
    static constexpr type_index_t index_direct_or_wrapper = is_direct ? direct_index : direct_type<value_type_wrapper, Types...>::index;
    static constexpr bool is_direct_or_wrapper = index_direct_or_wrapper != invalid_value;
    static constexpr type_index_t index = is_direct_or_wrapper ? index_direct_or_wrapper : convertible_type<value_type, Types...>::index;
    static constexpr bool is_valid = index != invalid_value;
    static constexpr type_index_t tindex = is_valid ? sizeof...(Types) - index : 0;
    using target_type = typename std::tuple_element<tindex, std::tuple<void, Types...>>::type;
};
} /** namespace internal_impl */
} /** namespace core */
} /** namespace ara */

#endif /** !__ARA_CORE_INTERNAL_VARIANT_INTERNAL_H__ */

